package com.tre.jdevtemplateboot.service.impl;

import com.tre.jdevtemplateboot.common.context.BaseContext;
import com.tre.jdevtemplateboot.common.dto.ResponseResult;
import com.tre.jdevtemplateboot.domain.dto.SpAppMstDto;
import com.tre.jdevtemplateboot.domain.po.SpAppMst;
import com.tre.jdevtemplateboot.domain.po.SpAppOssMst;
import com.tre.jdevtemplateboot.domain.po.SpCallbackMst;
import com.tre.jdevtemplateboot.domain.vo.SpAppMstListVO;
import com.tre.jdevtemplateboot.domain.vo.SpAppMstVO;
import com.tre.jdevtemplateboot.mapper.SpAppMstMapper;
import com.tre.jdevtemplateboot.mapper.SpAppOssMstMapper;
import com.tre.jdevtemplateboot.mapper.SpCallbackMstMapper;
import com.tre.jdevtemplateboot.service.ISpAppMstService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tre.jdevtemplateboot.service.ISpAppOssMstService;
import com.tre.jdevtemplateboot.service.ISpCallbackMstService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @author 熊一鸣
 * @since 2023-11-15
 */
@Service
public class SpAppMstServiceImpl extends ServiceImpl<SpAppMstMapper, SpAppMst> implements ISpAppMstService {

    @Autowired
    private SpAppMstMapper spAppMstMapper;

    @Qualifier("ISpAppOssMstService")
    private ISpAppOssMstService ISpAppOssMstService;

    @Qualifier("ISpCallbackMstService")
    private ISpCallbackMstService ISpCallbackMstService;

    @Autowired
    private SpAppOssMstMapper spAppOssMstMapper;

    @Autowired
    private SpCallbackMstMapper spCallbackMstMapper;

    @Override
    public ResponseResult<SpAppMstListVO> findAll() {
        List<SpAppMstListVO> listVO = spAppMstMapper.getList();
        return ResponseResult.buildOK(listVO);
    }

    @Override
    public ResponseResult<SpAppMstVO> getSpAppById(String id) {
        SpAppMst spAppMst = getById(id);
        String appId = String.valueOf(spAppMst.getId());
        // TODO 暂不开发
        if (spAppMst.getOssType().equals("1")) {
            List<SpAppOssMst> ossMsts = spAppOssMstMapper.getListByAppId(appId);
        }
        return ResponseResult.buildOK(spAppMst);
    }

    @Override
    public ResponseResult saveApp(SpAppMstDto dto) {
        SpAppMst spAppMst = new SpAppMst();
        BeanUtils.copyProperties(dto, spAppMst);
        // 处理阿里oss和回调
        handleOssAndCallback(dto);
        // 设置id、创建（编辑）人何时间
        spAppMst.setId(Integer.valueOf(UUID.randomUUID().toString().replace("-","")));
        spAppMst.setCreateDate(LocalDateTime.now());
        spAppMst.setEditDate(LocalDateTime.now());
        spAppMst.setCreatePerson(BaseContext.getCurrentId());
        spAppMst.setEditPerson(BaseContext.getCurrentId());
        return ResponseResult.buildOK();
    }

    @Override
    public ResponseResult updateApp(SpAppMstDto dto) {
        SpAppMst spAppMst = new SpAppMst();
        BeanUtils.copyProperties(dto, spAppMst);
        // 处理阿里oss和回调
        handleOssAndCallback(dto);
        spAppMst.setEditDate(LocalDateTime.now());
        spAppMst.setEditPerson(BaseContext.getCurrentId());
        updateById(spAppMst);
        return ResponseResult.buildOK();
    }

    @Override
    public ResponseResult delete(String id) {
        removeById(id);
        spAppOssMstMapper.removeByAppId(id);
        spCallbackMstMapper.removeByAppId(id);
        return ResponseResult.buildOK();
    }

    public void handleOssAndCallback(SpAppMstDto dto){
        // 判断是否启用阿里oss
        if (dto.getOssType().equals("1")) {
            spAppOssMstMapper.removeByAppId(dto.getId());
            List<SpAppOssMst> ossMsts = dto.getSpAppOssMsts();
            ossMsts.forEach(spAppOssMst -> {
                spAppOssMst.setId(UUID.randomUUID().toString().replace("-",""));
                spAppOssMst.setAppId(dto.getId());
            });
            ISpAppOssMstService.saveBatch(ossMsts);
        }
        // 判断是否启用回调
        if (dto.getType() == 1) {
            spCallbackMstMapper.removeByAppId(dto.getId());
            List<SpCallbackMst> callbackMsts = dto.getSpCallbackMsts();
            callbackMsts.forEach(spCallbackMst -> {
                spCallbackMst.setId(UUID.randomUUID().toString().replace("-",""));
                spCallbackMst.setAppId(dto.getId());
            });
            ISpCallbackMstService.saveBatch(callbackMsts);
        }
    }

}

